/*
 * @Author: 追逐的娃 kangchao_php@163.com
 * @Date: 2024-04-30 13:49:34
 * @LastEditors: 追逐的娃 kangchao_php@163.com
 * @LastEditTime: 2024-05-08 10:16:31
 * @FilePath: \huijie-admin\src\router\index.js
 * @Description: 
 * 
 */
import { createRouter, createWebHashHistory } from 'vue-router'


const routes = [
    {
        path: '/',
        name: 'layout',
        component: () => import('@/layout/index.vue'),
    }, {
        path: '/login',
        name: 'login',
        meta: { title: '登录' },
        component: () => import('@/views/login/index.vue')
    }
]


//动态路由
const asyncRoutes = [{
    path: '/',
    name: '/',
    component: () => import('@/views/index/index.vue'),
    meta: {
        title: '首页'
    }
},{
    path: '/access/list',
    name: '/access/list',
    component: () => import('@/views/manage/menus.vue'),
    meta: {
        title: '菜单管理'
    }
}]


export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//动态添加路由
export const addRoutes = (menus) => {
    //是否有新的路由
    let hasNewRoutes = false
    const findAndAddRoutesByMenus = (arr) => {
        arr.forEach(e => {
            let item = asyncRoutes.find(o => o.path == e.frontpath)
            if (item && !router.hasRoute(item.path)) {
                router.addRoute("layout", item)
                hasNewRoutes = true
              }
              if (e.child && e.child.length > 0) {
                findAndAddRoutesByMenus(e.child)
              }
        })
    }
    findAndAddRoutesByMenus(menus)
    return hasNewRoutes
}

